package com.example.ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ruiji.common.Result;
import com.example.ruiji.common.ThreadLocalId;
import com.example.ruiji.entity.Employee;
import com.example.ruiji.mapper.EmployMapper;
import com.example.ruiji.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployMapper employMapper;

    @Autowired
    HttpSession httpSession;

    @Override
    public Result<Employee> login(Employee employee) {
        String password = DigestUtils.appendMd5DigestAsHex(employee.getPassword().getBytes(), new StringBuilder()).toString();
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee employeeDB = employMapper.selectOne(lambdaQueryWrapper);
        if (employeeDB == null) {
            return Result.error("登录失败");
        }
        if (!password.equals(employeeDB.getPassword())) {
            return Result.error("密码错误");
        }
        if (employeeDB.getStatus() == 0) {
            return Result.error("用户已禁用");
        }
        httpSession.setAttribute("employee", employeeDB.getId());
        return Result.success(employeeDB);
    }

    @Override
    public Result<String> logout() {
        httpSession.removeAttribute("employee");
        return Result.success("退出成功");
    }

    @Override
    public Result<String> addEmployee(Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employMapper.insert(employee);

        return Result.success("新增成功");
    }

    @Override
    public Result<Page<Employee>> Page(Integer page, Integer pageSize, String name) {
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //若name不为空，再去查询和name相关的字段
        lambdaQueryWrapper.like(StringUtils.hasText(name), Employee::getName, name);
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        Page<Employee> employeePage = employMapper.selectPage(pageInfo, lambdaQueryWrapper);
        return Result.success(employeePage);
    }

    @Override
    public Result<String> updateEmployee(Employee employee) {
        employMapper.updateById(employee);
        return Result.success("修改成功");
    }

    @Override
    public Result<Employee> getById(Long id) {
        Employee employee = employMapper.selectById(id);
        if (!ObjectUtils.isEmpty(employee)) {
            return Result.success(employee);
        }
        return Result.error("用户不存在");
    }
}
